﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;

namespace LotteryAdmin
{
    public partial class ChildModifyMemberInfo : ChildWindow
    {
        public bool isHaveChanged=false;
        LotteryAdmin.AdminService.LotteryAdminClient client = WcfProxy.GetProxy;
        private string UserID;
        private static int seletedIndex = 0;
        public ChildModifyMemberInfo()
        {
            InitializeComponent();
            client.GetMemberInfoByUIDCompleted += new EventHandler<AdminService.GetMemberInfoByUIDCompletedEventArgs>(client_GetMemberInfoByUIDCompleted);
            client.GetMemberBankInfoByUIDCompleted += new EventHandler<AdminService.GetMemberBankInfoByUIDCompletedEventArgs>(client_GetMemberBankInfoByUIDCompleted);
            client.GetCapitalByUIDCompleted += new EventHandler<AdminService.GetCapitalByUIDCompletedEventArgs>(client_GetCapitalByUIDCompleted);
            client.UpdateCapitalByUIDCompleted += new EventHandler<AdminService.UpdateCapitalByUIDCompletedEventArgs>(client_UpdateCapitalByUIDCompleted);
            client.UpdateMemberBankInfoByUIDCompleted += new EventHandler<AdminService.UpdateMemberBankInfoByUIDCompletedEventArgs>(client_UpdateMemberBankInfoByUIDCompleted);
            client.UpdateMemberInfoByUIDCompleted += new EventHandler<AdminService.UpdateMemberInfoByUIDCompletedEventArgs>(client_UpdateMemberInfoByUIDCompleted);
            client.GetOverAllCfgCompleted += new EventHandler<GetOverAllCfgCompletedEventArgs>(client_GetOverAllCfgCompleted);
            this.Loaded += new RoutedEventHandler(ChildModifyMemberInfo_Loaded);
            
        }

        void client_GetOverAllCfgCompleted(object sender, GetOverAllCfgCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            if (e.Result.Keys.Contains("MaxBonusPct"))
            {
                nudBackPct.Maximum = Convert.ToDouble(e.Result["MaxBonusPct"]);
            }
            nudBackPct.Increment = 0.1;
            //if (e.Result.Keys.Contains("MemBonusIncrement"))
            //    nudBackPct.Increment = Convert.ToDouble(e.Result["MemBonusIncrement"]);
        }

        void ChildModifyMemberInfo_Loaded(object sender, RoutedEventArgs e)
        {
            if (memList == null)
            {
                btnModBankInfo.Visibility = Visibility.Collapsed;
                btnModBaseInfo.Visibility = Visibility.Collapsed;
                btnModGameInfo.Visibility = Visibility.Collapsed;
            }
            Bind();
        }
        MemberList org;
        MemberList memList;
        public void showChildWindow(AdminService.MemberList info)
        {
            UserID = info.UserID;
            org = info;
            memList = ModelCopy.CopyValueForModel<MemberList>(info);
            base.Show();
        }
        public void showChildWindowforView(AdminService.MoneyOutList info)
        {
            UserID = info.UserID;
            tabItem1.IsSelected = true;
            base.Show();
        }
        private void client_UpdateMemberInfoByUIDCompleted(object sender, AdminService.UpdateMemberInfoByUIDCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            org = ModelCopy.CopyValueForModel<MemberList>(memList,org);
            isHaveChanged = true;
            MessageBox.Show("修改基本信息成功");
        }

        private void client_UpdateMemberBankInfoByUIDCompleted(object sender, AdminService.UpdateMemberBankInfoByUIDCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            isHaveChanged = true;
            client.GetMemberBankInfoByUIDAsync(UserID);
            MessageBox.Show("修改银行信息成功");
        }

        private void client_UpdateCapitalByUIDCompleted(object sender, AdminService.UpdateCapitalByUIDCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            org.GameScores = Convert.ToDouble(txtGameScore.Text);
            isHaveChanged = true;
            MessageBox.Show("修改游戏信息成功");
        }

        private void client_GetCapitalByUIDCompleted(object sender, AdminService.GetCapitalByUIDCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            GameGrid.DataContext = e.Result;
        }

        private void client_GetMemberBankInfoByUIDCompleted(object sender, AdminService.GetMemberBankInfoByUIDCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            if (e.Result.Count == 0)
                btnModBankInfo.IsEnabled = false;
            else
            {
                cbxBankType.Tag = e.Result.ToList();
                cbxBankType.SelectedIndex = 0;
                BankGrid.DataContext = e.Result[0];
            }
        }

        private void client_GetMemberInfoByUIDCompleted(object sender, AdminService.GetMemberInfoByUIDCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            if (e.Result == null)
            {
                MessageBox.Show("账号不存在!");
                return;
            }
            txtUserID.Text = UserID;
            txtUserPwd.Text = e.Result.UserPwd;
            cbxStatus.SelectedIndex = e.Result.IsActive == 1 ? 0 : 1;
            nudBackPct.Value = e.Result.Bonus;
            cbxRole.SelectedIndex = e.Result.RoleID == 2 ? 0 : 1;

        }
        private void Bind()
        {
            loadingProgress.IsBusy = true;
            client.GetCapitalByUIDAsync(UserID);
            client.GetMemberBankInfoByUIDAsync(UserID);
            //client.GetMemberInfoByUIDAsync(UserID);
            //if (App.Session.ContainsKey("GlobleConfig") && App.Session["GlobleConfig"] != null)
            //{
            //    nudBackPct.Maximum
            //        = Convert.ToDouble((App.Session["GlobleConfig"] as Dictionary<string, string>)["MaxBonusPct"]);
            //    nudBackPct.Increment
            //        = Convert.ToDouble((App.Session["GlobleConfig"] as Dictionary<string, string>)["MemBonusIncrement"]);
            //}
            //else
            //{
            //    loadingProgress.IsBusy = true;
            //    client.GetOverAllCfgAsync();
            //}
            nudBackPct.Maximum = 10;
            nudBackPct.Increment = 0.1;
            if (memList == null)
            {
                loadingProgress.IsBusy = true;
                client.GetMemberInfoByUIDAsync(UserID);
                return;
            }
            txtUserID.Text = memList.UserID;
            txtUserPwd.Text = memList.UserPwd;
            cbxStatus.SelectedIndex = memList.IsActive == 1 ? 0 : 1;
            nudBackPct.Value = memList.Bonus;
            cbxRole.SelectedIndex = memList.RoleID == 2 ? 0 : 1;
        }
        private void btnModBaseInfo_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate.ValidateInputModify(txtUserPwd.Text.Trim()))
            {
                MessageBox.Show("密码请使用英文和数字组合！");
                return;
            }
            AdminService.MemberInfo minfo = new AdminService.MemberInfo();
            minfo.UserPwd = txtUserPwd.Text.Trim();
            minfo.RoleID = cbxRole.SelectedIndex == 0 ? 2 : 3;
            minfo.IsActive = cbxStatus.SelectedIndex == 0 ? 1 : 0;
            minfo.Bonus = nudBackPct.Value;

            memList.UserPwd = minfo.UserPwd;
            memList.RoleID = minfo.RoleID;
            memList.RoleName = (cbxRole.SelectedItem as ComboBoxItem).Content.ToString();
            memList.IsActive = minfo.IsActive;
            memList.Bonus = minfo.Bonus;
            loadingProgress.IsBusy = true;
            client.UpdateMemberInfoByUIDAsync(minfo, UserID);
        }

        private void btnModBankInfo_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate.IsZNumberModify(txtBankNum.Text.Trim()))
            {
                MessageBox.Show("银行账号应该全是数字！");
                return;
            }
            if (!Validate.ValidateNameModify(txtUserName.Text.Trim()))
            {
                MessageBox.Show("银行户名应该是数字、英汉！");
                return;
            }
            if (!Validate.ValidateInputModify(txtBankPwd.Text.Trim()))
            {
                MessageBox.Show("资金密码应该是英文与数字组合！");
                return;
            }
            if (cbxBankType.SelectedItem == null)
            {
                MessageBox.Show("请选择银行类型！");
                return;
            }
            AdminService.MemberBankInfo mbinfo = new AdminService.MemberBankInfo();
            mbinfo.BankPwd = txtBankPwd.Text.Trim();
            mbinfo.CardNo = txtBankNum.Text.Trim();
            mbinfo.UserName = txtUserName.Text.Trim();
            mbinfo.BankType = (cbxBankType.SelectedItem as ComboBoxItem).Content.ToString();
            client.UpdateMemberBankInfoByUIDAsync(mbinfo, UserID);
            loadingProgress.IsBusy = true;
        }

        private void btnModGameInfo_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate.IsZFloatModify(txtGameScore.Text.Trim()))
            {
                MessageBox.Show("游戏分数应该为数字！");
                return;
            }
            if (!Validate.IsZFloatModify(txtEnableScroe.Text.Trim()))
            {
                MessageBox.Show("可用分数应该为数字！");
                return;
            }
            if (!Validate.IsZFloatModify(txtDisableScroe.Text.Trim()))
            {
                MessageBox.Show("冻结分数应该为数字！");
                return;
            }
            AdminService.Capital cinfo = new AdminService.Capital();
            cinfo.GameScores = double.Parse(txtGameScore.Text.Trim());
            cinfo.EnableScores = double.Parse(txtEnableScroe.Text.Trim());
            cinfo.DisableScores = double.Parse(txtDisableScroe.Text.Trim());
            client.UpdateCapitalByUIDAsync(cinfo, UserID);
            loadingProgress.IsBusy = true;
        }

        private void cbxBankType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cbx = sender as ComboBox;
            if (cbx.Tag != null && cbx.SelectedItem != null)
            {
                List<AdminService.MemberBankInfo> list = cbx.Tag as List<AdminService.MemberBankInfo>;
                if (list == null)
                {
                    BankGrid.DataContext = new AdminService.MemberBankInfo();
                    return;
                }
                AdminService.MemberBankInfo info = list.Where(p => p.BankType == (cbx.SelectedItem as ComboBoxItem).Content.ToString()).FirstOrDefault();
                if (info == null)
                {
                    BankGrid.DataContext = info;
                    return;
                }
                BankGrid.DataContext = info;
                if (info.BankType.Trim() == "工商银行" || info.BankType.Trim() == "中国工商银行")
                    cbxBankType.SelectedIndex = 0;
                else if (info.BankType.Trim() == "财付通")
                    cbxBankType.SelectedIndex = 1;
            }
            else
            {
                txtBankNum.Text = string.Empty;
                txtUserName.Text = string.Empty;
                txtBankPwd.Text = string.Empty;
            }
        }
    }
}

